<?php

/**
 * Add body classes if certain regions have content.
 */

define('THEME_LAS_DIR_BASE', dirname(__FILE__));
define('THEME_LAS_DIR_TPL', THEME_LAS_DIR_BASE.'/templates');

function lasooo_preprocess_html(&$variables) {
  if (!empty($variables['page']['featured'])) {
    $variables['classes_array'][] = 'featured';
  }

  if (!empty($variables['page']['triptych_first'])
    || !empty($variables['page']['triptych_middle'])
    || !empty($variables['page']['triptych_last'])) {
    $variables['classes_array'][] = 'triptych';
  }

  if (!empty($variables['page']['footer_firstcolumn'])
    || !empty($variables['page']['footer_secondcolumn'])
    || !empty($variables['page']['footer_thirdcolumn'])
    || !empty($variables['page']['footer_fourthcolumn'])) {
    $variables['classes_array'][] = 'footer-columns';
  }

  // Add conditional stylesheets for IE
  drupal_add_css(path_to_theme() . '/css/960/960.css');
  drupal_add_css(path_to_theme() . '/css/960/test.css');
  drupal_add_css(path_to_theme() . '/css/960/reset.css');
  drupal_add_css(path_to_theme() . '/css/960/fonts.css');
  //drupal_add_css(path_to_theme() . '/css/ie6.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 6', '!IE' => FALSE), 'preprocess' => FALSE));
  
  
  
  if( true == $variables['logged_in'] ){
      drupal_add_css(path_to_theme() . '/css/headerbar.region.css');
  }else{
  	drupal_add_css(path_to_theme() . '/css/headerbar-unlogged.region.css');
  }
}

/**
 * Override or insert variables into the page template for HTML output.
 */
function lasooo_process_html(&$variables) {
  // Hook into color.module.
  if (module_exists('color')) {
    _color_html_alter($variables);
  }
}

/**
 * Override or insert variables into the page template.
 */
function lasooo_process_page(&$variables) {
  // Hook into color.module.
  if (module_exists('color')) {
    _color_page_alter($variables);
  }
  // Always print the site name and slogan, but if they are toggled off, we'll
  // just hide them visually.
  $variables['hide_site_name']   = theme_get_setting('toggle_name') ? FALSE : TRUE;
  $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE;
  if ($variables['hide_site_name']) {
    // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
    $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  }
  if ($variables['hide_site_slogan']) {
    // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we rebuild it.
    $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
  }
  // Since the title and the shortcut link are both block level elements,
  // positioning them next to each other is much simpler with a wrapper div.
  if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
    // Add a wrapper div using the title_prefix and title_suffix render elements.
    $variables['title_prefix']['shortcut_wrapper'] = array(
      '#markup' => '<div class="shortcut-wrapper clearfix">',
      '#weight' => 100,
    );
    $variables['title_suffix']['shortcut_wrapper'] = array(
      '#markup' => '</div>',
      '#weight' => -99,
    );
    // Make sure the shortcut link is the first item in title_suffix.
    $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
  }
  
  // Load special style by page.
  if($variables['page'] && isset($variables['node']) && 'activity' == $variables['node']->type){
      drupal_add_css(drupal_get_path('theme', 'lasooo') . '/css/page-activity-detail.css');
  }
  
  $variables['breadcrumb'] = drupal_get_breadcrumb();
  
  _lasooo_preset_usermenus($variables);
  _lasooo_process_layout($variables);
  _lasooo_load_stylesheetByLayout($variables);
  _lasooo_load_javascript($variables);
}

function _lasooo_show_breadcrumb($breadcrumb){
	if (!is_array($breadcrumb) || 1>count($breadcrumb)) {
		return null;
	}
    $re = '';
    foreach ($breadcrumb as $link) {
    	$re .= empty($re) ? $link : '&nbsp;»&nbsp;'.$link;
    }
    return $re;
}

function _lasooo_load_javascript(&$vars){
    drupal_add_js(path_to_theme() . '/js/light-framework.js');
	if(true == $vars['is_front']){
        drupal_add_js(path_to_theme() . '/js/page-front.js');
    }
    
   
}

function _lasooo_process_layout(&$vars){
	if ($vars['is_front']) {
		$vars['layout'] = 'frontpage';
        return true;
	}
    
    $path = request_path();
    if (1 === preg_match('/^[a-z]+$/', $path) && file_exists(dirname(__FILE__).'/templates/layouts/'.$path.'.layout.php')) {
    	$vars['layout'] = $path;
    }else{
    	$vars['layout'] = 'default';
    }
    
    drupal_alter('layout', $vars['layout']);
}

function _lasooo_preset_usermenus(&$vars){
    global $user;
    if( true==$vars['logged_in'] ){
        $userAccount = array(
            'attributes'=> array('title'=>'My Account'),
            'href'=> '/users/'.strtolower(str_replace(' ', '-', $user->name)),
            'title' => '<span class="username">'.strip_tags($user->name).'</span>' . member_load_member_photo($user),
            'html' => true,
        );
        $myMessages = array(
            'attributes'=> array('title'=>'Messages'),
            'href'=> '/home/messages',
            'title' => '消息(0)'
        );
        $userHome = array(
            'attributes'=> array('title'=>'我的主页'),
            'href'=> '/home',
            'title' => t('我的书屋')
        );
        
        $myActivities = array(
            'attributes' => array('title'=>'我的活动'),
            'href' => '/home/myactivity',
            'title' => t('我的活动')
        );
        
        $publishActivities = array(
            'attributes' => array('title'=>'发布活动'),
            'href' => '/activity/create',
            'title' => t('发布活动')
        );
        
        $logout = array(
            'attributes' => array('title'=>'退出'),
            'href' => '/user/logout',
            'title' => t('退出')
        );
        
        array_unshift($vars['main_menu'], $logout, $userAccount); //, $userHome, $myActivities, $publishActivities
    }else{
        $login = array(
            'attributes'=> array('title'=>'Login'),
            'href'=> '/user/login',
            'title' => t('登陆')
        );
        $register = array(
            'attributes'=> array('title'=>'Register'),
            'href'=> '/user/register',
            'title' => t('注册')
        );
        array_unshift($vars['main_menu'], $login, $register);
    }
}

function _lasooo_load_stylesheetByLayout(&$vars){
	$layout = $vars['layout'];
    
    if (file_exists(dirname(__FILE__).'/css/layouts/'.$layout.'.layout.css')) {
    	drupal_add_css(path_to_theme().'/css/layouts/'.$layout.'.layout.css');
    }
    
    if (!empty($vars['node'])) {
    	$nodeType = $vars['node']->type;
        
        if (!file_exists(path_to_theme().'/css/'.$nodeType.'.node.css')) {
        	drupal_add_css(path_to_theme().'/css/default.node.css');
        } else {
        	drupal_add_css(path_to_theme().'/css/'.$nodeType.'.node.css');
        }
    }
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function lasooo_preprocess_maintenance_page(&$variables) {
  if (!$variables['db_is_active']) {
    unset($variables['site_name']);
  }
  drupal_add_css(drupal_get_path('theme', 'lasooo') . '/css/maintenance-page.css');
}

/**
 * Override or insert variables into the maintenance page template.
 */
function lasooo_process_maintenance_page(&$variables) {
  // Always print the site name and slogan, but if they are toggled off, we'll
  // just hide them visually.
  $variables['hide_site_name']   = theme_get_setting('toggle_name') ? FALSE : TRUE;
  $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE;
  if ($variables['hide_site_name']) {
    // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
    $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  }
  if ($variables['hide_site_slogan']) {
    // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we rebuild it.
    $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
  }
}

/**
 * Override or insert variables into the node template.
 */
function lasooo_preprocess_node(&$variables) {
  if ($variables['view_mode'] == 'full' && node_is_page($variables['node'])) {
    $variables['classes_array'][] = 'node-full';
  }
}

/**
 * Override or insert variables into the block template.
 */
function lasooo_preprocess_block(&$variables) {
  // In the header region visually hide block titles.
  if ($variables['block']->region == 'header') {
    $variables['title_attributes_array']['class'][] = 'element-invisible';
  }
}

/**
 * Implements theme_menu_tree().
 */
function lasooo_menu_tree($variables) {
  return '<ul class="menu clearfix">' . $variables['tree'] . '</ul>';
}

/**
 * Implements theme_field__field_type().
 */
function lasooo_field__taxonomy_term_reference($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<h3 class="field-label">' . $variables['label'] . ': </h3>';
  }

  // Render the items.
  $output .= ($variables['element']['#label_display'] == 'inline') ? '<ul class="links inline">' : '<ul class="links">';
  foreach ($variables['items'] as $delta => $item) {
    $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</li>';
  }
  $output .= '</ul>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . (!in_array('clearfix', $variables['classes_array']) ? ' clearfix' : '') . '">' . $output . '</div>';

  return $output;
}
